package com.rzjm.api.netty.water.business.inhandler;

import com.rzjm.api.netty.water.NettyUtil;
import com.rzjm.api.netty.water.entity.ReceiveData;
import com.rzjm.api.service.IDeviceWaterMessagesService;
import com.rzjm.api.service.IDeviceWaterService;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author WangWT
 * @Description:
 * @date 2023/4/14 9:27
 */
@Component
public class ReceiveCommonInHandler implements InHandler {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReceiveCommonInHandler.class);
    @Autowired
    private IDeviceWaterService deviceWaterService;
    @Autowired
    private IDeviceWaterMessagesService waterMessagesService;


    @Override
    public void handler(ChannelHandlerContext ctx, ReceiveData receiveData) {
        //保存Channel
        NettyUtil.addChannel(ctx, receiveData.getId());
        // 保存上报数据日志
        Optional.ofNullable(getMessage(receiveData))
            .ifPresent(message -> waterMessagesService.save(message));
        String replyData;
        //回复消息
        replyData = "{\"rt\":97,\"v1\":"+ receiveData.getType() +",\"v2\":0}";
        reply(replyData, receiveData.getId());
    }
}
